package toolCategories

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.material.Icon
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.runBlocking
import org.jetbrains.compose.resources.getString
import org.jetbrains.compose.resources.painterResource
import tool.Tool
import tool.ToolEvent
import tool.encode.*
import top.mcwebsite.devtoolbox.devtoolbox.generated.resources.Res
import top.mcwebsite.devtoolbox.devtoolbox.generated.resources.code_assistant
import top.mcwebsite.devtoolbox.devtoolbox.generated.resources.encode

object CodeAssistantToolCategory : ToolCategory {

    private val _name: String by lazy {
        runBlocking {
            getString(Res.string.code_assistant)
        }
    }

    override fun getName(): String {
        return _name
    }

    override fun getTools(): List<Tool> {
        return listOf(URLEncodeTool, URLDecodeTool, Base64EncodeTool, Base64DecodeTool, MD5EncodeTool, SHAEncodeTool)
    }

    @Composable
    override fun Render() {
        var currentTools by remember { mutableStateOf<Tool?>(null) }
        currentTools?.Render(
            onEvent = {
                when (it) {
                    is ToolEvent.BackEvent -> {
                        currentTools = null
                    }
                }
            }
        ) ?: run {
            LazyVerticalGrid(
                columns = GridCells.Adaptive(200.dp),
                horizontalArrangement = Arrangement.spacedBy(10.dp),
                verticalArrangement = Arrangement.spacedBy(10.dp),
            ) {
                items(getTools().size) {
                    val tool = getTools()[it]
                    ToolItem(tool) {
                        currentTools = tool
                    }
                }
            }
        }

    }

    @Composable
    override fun ToolCategoryIcon(modifier: Modifier) {
        Icon(painterResource(Res.drawable.encode), contentDescription = "Code Assistant", modifier)
    }
}